package com.yhl.vocation.controller;

import com.yhl.vocation.entity.pojo.Student;
import com.yhl.vocation.service.StudentService;
import com.yhl.vocation.util.ResUtil;
import com.yhl.vocation.util.WxUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

@RestController
@RequestMapping("user")
/**
 * @Description
 * @Author Date 2023/2/27-19:32
 */
public class UserController {
    private StudentService studentService;

    public UserController(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping("wxLogin")
    public String wxLogin(@RequestParam("code") String code) throws UnsupportedEncodingException {
        String openId = WxUtil.getOpenIdByCode(code);
        Student student = studentService.getInfo(openId);
        if (student != null)
            return ResUtil.build("200",openId, student);
        else
            return ResUtil.build("4001",openId);
    }

    @GetMapping("bind")
    public String bind(@RequestParam("openid")String openid, @RequestParam("studentNum")String studentNum, @RequestParam("idCard")String idCard){
        if(studentService.bind(studentNum,idCard,openid)){
            return ResUtil.build("600","绑定成功");
        }else {
            return ResUtil.build("600", "信息绑定失败");
        }
    }
}
